package com.zwz.mall.managers;

import android.util.Log;

import com.zwz.mall.api.BaseApiService;
import com.zwz.mall.app.BaseApplication;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @Author: EzerXie
 * @Description:Description: 接口域名管理器.
 * @Date: Created on 2018/11/6 15:04
 * @Modified By:
 */
public class HttpRequestManager {
    private BaseApiService mBaseApiService;
    private static final int DEFAULT_TIMEOUT = 30;
    private static volatile HttpRequestManager sInst = null;
    private OkHttpClient mOkHttpClient;



    public static HttpRequestManager getInstance() {
        HttpRequestManager inst = sInst;  // <<< 在这里创建临时变量
        if (inst == null) {
            synchronized (HttpRequestManager.class) {
                inst = sInst;
                if (inst == null) {
                    inst = new HttpRequestManager();
                    sInst = inst;
                }
            }
        }
        return inst;  // <<< 注意这里返回的是临时变量
    }

    private void init() {
        mOkHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {


            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request.Builder builder1 = request.newBuilder();

                builder1.addHeader("os", "Android");//添加请求头
                Request build = builder1.build();
                return chain.proceed(build);
            }
        }).connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();
        mBaseApiService = new Retrofit.Builder().baseUrl(BaseApplication.getApplication().getHost())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(
                        RxJava2CallAdapterFactory.create())
                .client(mOkHttpClient)
                .build()
                .create(BaseApiService.class);
    }


    public Map<String, String> getHeaders() {
        Map<String, String> mHeaders = new HashMap<>();

        return mHeaders;
    }

    public Map<String, String> getHeaders(String apiV) {
        Map<String, String> mHeaders = new HashMap<>();

        return mHeaders;
    }

    private HttpRequestManager() {
        init();
    }

    public BaseApiService getApiService() {
        return mBaseApiService;
    }


}
